/**
 * 计算当前时间与传入时间的差值天、小时、秒
 * 例如 当前时间 2024-06-06 14:10:26"
 * 传入 targetTime:2024-06-06 14:10:00 ==> 26秒
 * 传入 targetTime:2024-06-06 15:10:00 ==> 1小时26秒
 * 传入 targetTime:2024-06-07 15:10:00 ==> 1天1小时
 *
 * @param Date targetTime
 * @returns {string}
 */
const getDiffTime = function (targetTime: any) {
    if (!(targetTime instanceof Date)) {
        console.warn("getDiffTime failed, targetTime is not instance of Date");
        return '';
    }
    const currentTime = new Date();
    // 相差秒数
    const diffSeconds = Math.abs(Math.floor((currentTime.getTime() - targetTime.getTime()) / 1000));
    // 计算天数
    const daysDiff = Math.floor(diffSeconds / (60 * 60 * 24));
    // 计算小时数
    const hoursDiff = Math.floor(diffSeconds % (60 * 60 * 24) / (60 * 60));
    // 计算分钟数
    const minutesDiff = Math.floor(diffSeconds % (60 * 60) / 60);
    // 计算秒
    const secondsDiff = diffSeconds % 60;

    if (diffSeconds > 60 * 60 * 24 * 365) { //超过1年
        return "超过1年";
    } else if (diffSeconds > 60 * 60 * 24) {  //超过1天
        return `${daysDiff}天${hoursDiff}小时`;
    } else if (diffSeconds > 60 * 60) {  //超过1小时
        return `${hoursDiff}小时${minutesDiff}分`;
    } else if (diffSeconds > 60) {    //超过1分钟
        return `${minutesDiff}分${secondsDiff}秒`;
    } else if (diffSeconds > 0) {
        return `${secondsDiff}秒`;
    } else {
        return '';
    }
}

const getTimeDifferenceText = (timeStr: any) => {
    const currentTime = new Date();
    const targetTime = new Date(timeStr.replace(/-/g, "/"));

    const year = targetTime.getFullYear();
    const month = (targetTime.getMonth() + 1).toString().padStart(2, '0');
    const day = targetTime.getDate().toString().padStart(2, '0');
    const hours = targetTime.getHours().toString().padStart(2, '0');
    const minutes = targetTime.getMinutes().toString().padStart(2, '0');

    if (currentTime.getFullYear() !== targetTime.getFullYear()) {
        // 年份不同，输出年月日时分
        return `${year}-${month}-${day} ${hours}:${minutes}前`;
    } else {
        const timeDifference = targetTime.getTime() - currentTime.getTime();
        const minutesDifference = Math.floor(timeDifference / (1000 * 60));
        if (Math.abs(timeDifference) >= 24 * 60 * 60 * 1000) {
            // 时间差大于1天，输出月日时分
            return `${month}-${day} ${hours}:${minutes}前`;
        } else if (minutesDifference < 120) {
            // 时间差小于2小时，输出分钟数
            return `${minutesDifference}分钟内`;
        } else if (targetTime.getDate() === currentTime.getDate() + 1) {
            // 相差一天，输出“明日”+时分
            return `明日 ${hours}:${minutes}前`;
        } else {
            return `${hours}:${minutes}前`;
        }
    }
}

const getTimeText = (timeStr: string) => {
    const currentTime = new Date();
    const targetTime = new Date(timeStr.replace(/-/g, "/"));
    const timeDiff = currentTime.getTime() - targetTime.getTime();
    // 计算天数
    const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
    const remainingTime1 = timeDiff % (1000 * 60 * 60 * 24);

    // 计算小时数
    const hoursDiff = Math.floor(remainingTime1 / (1000 * 60 * 60));
    const remainingTime2 = remainingTime1 % (1000 * 60 * 60);

    // 计算分钟数
    const minutesDiff = Math.floor(remainingTime2 / (1000 * 60));

    if (daysDiff > 0) {
        return `已超时 ${daysDiff}天${hoursDiff > 0 ? `${hoursDiff}时${minutesDiff > 0 ? `${minutesDiff}分` : ''}` : ''}`;
    } else if (hoursDiff > 0) {
        return `已超时 ${hoursDiff}时${minutesDiff > 0 ? `${minutesDiff}分` : ''}`;
    } else if (minutesDiff > 0) {
        return `已超时 ${minutesDiff}分`;
    } else {
        return `已超时 ${minutesDiff}分`;
    }
}

const getDiffTimeText = function (targetTime: any) {
    if (!(targetTime instanceof Date)) {
        console.warn("getDiffTime failed, targetTime is not instance of Date");
        return '';
    }
    const currentTime = new Date();
    // 相差秒数
    const diffSeconds = Math.abs(Math.floor((currentTime.getTime() - targetTime.getTime()) / 1000));
    // 计算天数
    const daysDiff = Math.floor(diffSeconds / (60 * 60 * 24));
    // 计算小时数
    const hoursDiff = Math.floor(diffSeconds % (60 * 60 * 24) / (60 * 60));
    // 计算分钟数
    const minutesDiff = Math.floor(diffSeconds % (60 * 60) / 60);
    // 计算秒
    const secondsDiff = diffSeconds % 60;

    if (diffSeconds > 60 * 60 * 24 * 365) { //超过1年
        return "1年";
    } else if (diffSeconds > 60 * 60 * 24) {  //超过1天
        return `${daysDiff}天`;
    } else if (diffSeconds > 60 * 60) {  //超过1小时
        return `${hoursDiff}时`;
    } else if (diffSeconds > 60) {    //超过1分钟
        return `${minutesDiff}:${secondsDiff}`;
    } else if (diffSeconds > 0) {
        return `00:${secondsDiff}`;
    } else {
        return '';
    }
}

/**
 * 传入时间是否大于当前时间
 * @param Date targetTime
 * @returns {boolean}
 */
const greaterThanCurTime = function (targetTime: any) {
    if (!(targetTime instanceof Date)) {
        console.warn("greaterCurTime failed, targetTime is not instance of Date");
        return false;
    }
    return targetTime.getTime() > new Date().getTime();

}

const formatTimeRangeText = (startTime: any, endTime: any) => {
    const now = new Date();
    const currentYear = now.getFullYear();
    const currentMonth = now.getMonth() + 1;
    const currentDate = now.getDate();

    if ((!startTime) && endTime) {
        return endTime.slice(5);
    }

    if (!endTime && startTime) {
        return startTime.slice(5);
    }

    if (startTime == null && endTime == null) {
        return ''
    }

    const start = new Date(startTime.replace(/-/g, "/"));
    const end = new Date(endTime.replace(/-/g, "/"));

    if (isNaN(start.getTime()) || isNaN(end.getTime())) {
        return '';
    }

    const startMonth = start.getMonth() + 1;
    const startDate = start.getDate();
    const endMonth = end.getMonth() + 1;
    const endDate = end.getDate();

    let startTimeStr = `${start.getHours()}:${start.getMinutes().toString().padStart(2, '0')}`;
    let endTimeStr = `${end.getHours()}:${end.getMinutes().toString().padStart(2, '0')}`;

    const startDateStr = `${startMonth}-${startDate} ${startTimeStr}`;
    const endDateStr = `${endMonth}-${endDate} ${endTimeStr}`;

    // 开始时间与结束时间完全一致则只返回一个
    if (start.getFullYear() === end.getFullYear() &&
        start.getMonth() === end.getMonth() &&
        start.getDate() === end.getDate() &&
        start.getHours() === end.getHours() &&
        start.getMinutes() === end.getMinutes()) {
        return startDateStr;
    }



    if (start.getFullYear() === end.getFullYear()) {
        if (start.getFullYear() === currentYear) {
            if (startMonth === endMonth && endMonth === currentMonth && start.getDate() === end.getDate() && end.getDate() === currentDate) {
                return `${startTimeStr}-${endTimeStr}`;
            } else {
                return `${startDateStr}-${endTimeStr}`;
            }
        } else {
            return `${start.getFullYear()}-${startDateStr}-${endTimeStr}`;
        }
    } else {
        return `${start.getFullYear()}-${startDateStr} - ${end.getFullYear()}-${endDateStr}`;
    }
}

const isToday = (str: string) => {
    var d = new Date(str.replace(/-/g, "/"));
    var todaysDate = new Date();
    if (d.setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0)) {
        return formatText(str);
    } else {
        var timearr = str.replace(" ", ":").replace(/\:/g, "-").split("-");
        if (d.getFullYear() == todaysDate.getFullYear()) {
            return timearr[1] + '-' + timearr[2] + ' ' + timearr[3] + ":" + timearr[4]
        } else {
            return timearr[0] + '-' + timearr[1] + '-' + timearr[2] + ' ' + timearr[3] + ":" + timearr[4]
        }
    }
}

const formatText = (times: string) => {
    var timearr = times.replace(" ", ":").replace(/\:/g, "-").split("-");
    return timearr[3] + ":" + timearr[4]
}

const formatPickTime = function (time: any) {
    let n = new Date().getTime() - new Date(time).getTime();
    let day = Math.floor(n / 1000 / 60 / 60 / 24);
    let hour = Math.floor(n / 1000 / 60 / 60) % 24;
    let minute = Math.floor(n / 1000 / 60) % 60;
    let number = day * 24 * 60 + hour * 60 + minute;
    if (n >= 0 && !!time) {
        return number
    } else {
        return -1
    }
}

export {
    getDiffTime,
    greaterThanCurTime,
    formatTimeRangeText,
    getDiffTimeText,
    getTimeText,
    getTimeDifferenceText,
    isToday,
    formatText,
    formatPickTime
}
